package info.yaohua.servlet;

import info.yaohua.dao.Ac01Dao;
import info.yaohua.dto.Ac01Dto;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String username;   
	
	public void init(ServletConfig config)
	{
		username = config.getInitParameter("savePath");
	}
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("utf-8");
		String loginMessage;
		HttpSession session = request.getSession();
		String aac001 = request.getParameter("aac001");
		String aac005 = request.getParameter("aac005");
		
		Ac01Dao ac01Dao = new Ac01Dao();
		Ac01Dto ac01Dto = new Ac01Dto();
		ac01Dto.setAac001(aac001);
		ac01Dto.setAac005(aac005);
		try {
			ac01Dto = ac01Dao.doFindAc01ByAac001_Aac005(ac01Dto);
			if (ac01Dto!=null)
			{
				if(ac01Dto.getAac001().equals(username))
				{
					loginMessage = ac01Dto.getAac003().toString();
					request.setAttribute("loginMessage", loginMessage);
					request.removeAttribute("registerMessage");
					session.setAttribute("ac01", ac01Dto);
					RequestDispatcher view = request.getRequestDispatcher("/admin.jsp") ;
					view.forward(request, response);
				}
				else
				{
					loginMessage = ac01Dto.getAac003().toString();
					request.setAttribute("loginMessage", loginMessage);
					request.removeAttribute("registerMessage");
					session.setAttribute("ac01", ac01Dto);
					RequestDispatcher view = request.getRequestDispatcher("/admin.jsp") ;
					view.forward(request, response);
				}
				
			}
			else
			{
				loginMessage = "用户名或密码错误";
				request.setAttribute("loginMessage", loginMessage);
				request.removeAttribute("registerMessage");
				RequestDispatcher view = request.getRequestDispatcher("/login.jsp");
				view.forward(request, response);
			}
		} catch (NamingException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
